package rpg.writer.app.player;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import rpg.client.model.Actor;
import rpg.client.model.Dialog;
import rpg.client.model.DialogResponse;

public class DialogJPanel extends JDialog implements ActionListener {

    /**
	 * 
	 */
    private static final long serialVersionUID = 8964071458764451321L;
    private String storyID = "";

    private String chatID = "";

    public DialogJPanel(Dialog dialog, Actor actor) {
        super((java.awt.Frame) null, "Dialog", true);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        final AvatarPanel avatarPanel = new AvatarPanel(actor);
        add(avatarPanel, BorderLayout.WEST);

        final InfoPanel info = new InfoPanel(dialog, actor);
        add(info, BorderLayout.EAST);

        final ResponsePanel responses = new ResponsePanel(this, dialog);
        add(responses, BorderLayout.SOUTH);

        pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof ResponseButton) {
            ResponseButton rb = (ResponseButton) e.getSource();
            storyID = rb.getStoryID();
            chatID = rb.getChatID();
        }

        setVisible(false);
        dispose();
    }

    public String getChatID() {
        return chatID;
    }

    public String getStoryID() {
        return storyID;
    }
}

class InfoPanel extends JPanel {
    /**
	 * 
	 */
    private static final long serialVersionUID = 3743933813198887346L;

    InfoPanel(Dialog dialog, Actor actor) {
        super(new BorderLayout());
        add(new JLabel(dialog.getText()), BorderLayout.CENTER);
        add(new JLabel("[" + dialog.getMode() + "]"), BorderLayout.SOUTH);
    }
}

class ResponseButton extends JButton {
    /**
	 * 
	 */
    private static final long serialVersionUID = 7131414750777875613L;

    private final String storyID;
    private final String chatID;

    public ResponseButton(ActionListener listener, DialogResponse dr) {
        this(listener, dr.getStoryID(), dr.getChatID(), dr.getText());
    }

    public ResponseButton(ActionListener listener, String storyID, String chatID, String text) {
        super(text);
        this.storyID = storyID;
        this.chatID = chatID;
        addActionListener(listener);
    }

    String getChatID() {
        return chatID;
    }

    String getStoryID() {
        return storyID;
    }
}

class ResponsePanel extends JPanel {
    /**
	 * 
	 */
    private static final long serialVersionUID = 439610329063779620L;

    ResponsePanel(ActionListener listener, Dialog dialog) {
        super(new GridLayout(Math.max(1, dialog.getResponses().size()), 1));
        if (dialog.getResponses().isEmpty()) {
            add(new ResponseButton(listener, "", "", "..."));
        }
        else {
            for (DialogResponse dr : dialog.getResponses()) {
                ResponseButton rb = new ResponseButton(listener, dr);
                add(rb);
            }
        }
    }
}